{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# matplotlib cheat sheet\n",
    "\n",
    ">https://matplotlib.org/cheatsheets/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "# 输出 svg，更清晰\n",
    "%config InlineBackend.figure_format = 'svg'\n",
    "\n",
    "from decimal import Decimal\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "PI2 = 2 * np.pi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 坐标轴经过原点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def set_ticks_pass_zero(axes):\n",
    "    # 隐藏掉右边和上边的坐标轴\n",
    "    # 这里 spine 是脊柱的意思，代指坐标轴（Spines，一条线段上有一系列的凸起，是不是很像脊柱骨啊~）\n",
    "    axes.spines['right'].set_color('none')\n",
    "    axes.spines['top'].set_color('none')\n",
    "    \n",
    "    # 将 bottom/left 的 spines 设为 x/y 坐标轴\n",
    "    axes.xaxis.set_ticks_position('bottom')\n",
    "    axes.yaxis.set_ticks_position('left')\n",
    "    \n",
    "    # 使 spines 都经过原点\n",
    "    axes.spines['bottom'].set_position('zero')  # 即 y 的 data 为 0 的位置\n",
    "    axes.spines['left'].set_position('zero')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11c628fd0>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg height=\"232.818859pt\" version=\"1.1\" viewBox=\"0 0 349.2 232.818859\" width=\"349.2pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2022-05-28T17:32:43.353432</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.4.3, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linecap:butt;stroke-linejoin:round;}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 232.818859 \n",
       "L 349.2 232.818859 \n",
       "L 349.2 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 7.2 224.64 \n",
       "L 342 224.64 \n",
       "L 342 7.2 \n",
       "L 7.2 7.2 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"mba0373910e\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"14.744775\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- −6 -->\n",
       "      <g transform=\"translate(7.373681 130.518438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 678 2272 \n",
       "L 4684 2272 \n",
       "L 4684 1741 \n",
       "L 678 1741 \n",
       "L 678 2272 \n",
       "z\n",
       "\" id=\"DejaVuSans-2212\" transform=\"scale(0.015625)\"/>\n",
       "        <path d=\"M 2113 2584 \n",
       "Q 1688 2584 1439 2293 \n",
       "Q 1191 2003 1191 1497 \n",
       "Q 1191 994 1439 701 \n",
       "Q 1688 409 2113 409 \n",
       "Q 2538 409 2786 701 \n",
       "Q 3034 994 3034 1497 \n",
       "Q 3034 2003 2786 2293 \n",
       "Q 2538 2584 2113 2584 \n",
       "z\n",
       "M 3366 4563 \n",
       "L 3366 3988 \n",
       "Q 3128 4100 2886 4159 \n",
       "Q 2644 4219 2406 4219 \n",
       "Q 1781 4219 1451 3797 \n",
       "Q 1122 3375 1075 2522 \n",
       "Q 1259 2794 1537 2939 \n",
       "Q 1816 3084 2150 3084 \n",
       "Q 2853 3084 3261 2657 \n",
       "Q 3669 2231 3669 1497 \n",
       "Q 3669 778 3244 343 \n",
       "Q 2819 -91 2113 -91 \n",
       "Q 1303 -91 875 529 \n",
       "Q 447 1150 447 2328 \n",
       "Q 447 3434 972 4092 \n",
       "Q 1497 4750 2381 4750 \n",
       "Q 2619 4750 2861 4703 \n",
       "Q 3103 4656 3366 4563 \n",
       "z\n",
       "\" id=\"DejaVuSans-36\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"68.02985\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- −4 -->\n",
       "      <g transform=\"translate(60.658756 130.518438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 2419 4116 \n",
       "L 825 1625 \n",
       "L 2419 1625 \n",
       "L 2419 4116 \n",
       "z\n",
       "M 2253 4666 \n",
       "L 3047 4666 \n",
       "L 3047 1625 \n",
       "L 3713 1625 \n",
       "L 3713 1100 \n",
       "L 3047 1100 \n",
       "L 3047 0 \n",
       "L 2419 0 \n",
       "L 2419 1100 \n",
       "L 313 1100 \n",
       "L 313 1709 \n",
       "L 2253 4666 \n",
       "z\n",
       "\" id=\"DejaVuSans-34\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"121.314925\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- −2 -->\n",
       "      <g transform=\"translate(113.943831 130.518438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 1228 531 \n",
       "L 3431 531 \n",
       "L 3431 0 \n",
       "L 469 0 \n",
       "L 469 531 \n",
       "Q 828 903 1448 1529 \n",
       "Q 2069 2156 2228 2338 \n",
       "Q 2531 2678 2651 2914 \n",
       "Q 2772 3150 2772 3378 \n",
       "Q 2772 3750 2511 3984 \n",
       "Q 2250 4219 1831 4219 \n",
       "Q 1534 4219 1204 4116 \n",
       "Q 875 4013 500 3803 \n",
       "L 500 4441 \n",
       "Q 881 4594 1212 4672 \n",
       "Q 1544 4750 1819 4750 \n",
       "Q 2544 4750 2975 4387 \n",
       "Q 3406 4025 3406 3419 \n",
       "Q 3406 3131 3298 2873 \n",
       "Q 3191 2616 2906 2266 \n",
       "Q 2828 2175 2409 1742 \n",
       "Q 1991 1309 1228 531 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(171.41875 130.518438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 2034 4250 \n",
       "Q 1547 4250 1301 3770 \n",
       "Q 1056 3291 1056 2328 \n",
       "Q 1056 1369 1301 889 \n",
       "Q 1547 409 2034 409 \n",
       "Q 2525 409 2770 889 \n",
       "Q 3016 1369 3016 2328 \n",
       "Q 3016 3291 2770 3770 \n",
       "Q 2525 4250 2034 4250 \n",
       "z\n",
       "M 2034 4750 \n",
       "Q 2819 4750 3233 4129 \n",
       "Q 3647 3509 3647 2328 \n",
       "Q 3647 1150 3233 529 \n",
       "Q 2819 -91 2034 -91 \n",
       "Q 1250 -91 836 529 \n",
       "Q 422 1150 422 2328 \n",
       "Q 422 3509 836 4129 \n",
       "Q 1250 4750 2034 4750 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"227.885075\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(224.703825 130.518438)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"281.17015\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- 4 -->\n",
       "      <g transform=\"translate(277.9889 130.518438)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"334.455225\" xlink:href=\"#mba0373910e\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 6 -->\n",
       "      <g transform=\"translate(331.273975 130.518438)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"mc3d8f62053\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"219.739952\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- −6 -->\n",
       "      <g transform=\"translate(152.857812 223.539171)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"185.133302\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- −4 -->\n",
       "      <g transform=\"translate(152.857812 188.93252)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"150.526651\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <!-- −2 -->\n",
       "      <g transform=\"translate(152.857812 154.32587)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"115.92\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(161.2375 119.719219)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_12\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"81.313349\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(161.2375 85.112568)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_6\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"46.706698\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_13\">\n",
       "      <!-- 4 -->\n",
       "      <g transform=\"translate(161.2375 50.505917)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_7\">\n",
       "     <g id=\"line2d_14\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"174.6\" xlink:href=\"#mc3d8f62053\" y=\"12.100048\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_14\">\n",
       "      <!-- 6 -->\n",
       "      <g transform=\"translate(161.2375 15.899266)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_15\">\n",
       "    <path clip-path=\"url(#p861e7116c6)\" d=\"M 7.2 115.92 \n",
       "L 16.524888 109.986728 \n",
       "L 21.58697 107.023738 \n",
       "L 25.849776 104.772892 \n",
       "L 29.579731 103.035198 \n",
       "L 33.043261 101.646734 \n",
       "L 36.506791 100.499149 \n",
       "L 39.703896 99.670458 \n",
       "L 42.901 99.07548 \n",
       "L 46.098105 98.722772 \n",
       "L 49.028784 98.61668 \n",
       "L 51.959463 98.719747 \n",
       "L 54.890142 99.030727 \n",
       "L 58.087247 99.602619 \n",
       "L 61.284351 100.4092 \n",
       "L 64.481456 101.438868 \n",
       "L 67.944985 102.78884 \n",
       "L 71.674941 104.489749 \n",
       "L 75.671321 106.559382 \n",
       "L 80.200553 109.156393 \n",
       "L 86.061911 112.795084 \n",
       "L 103.113135 123.577075 \n",
       "L 107.642366 126.091883 \n",
       "L 111.638747 128.069466 \n",
       "L 115.368702 129.669841 \n",
       "L 118.832232 130.915575 \n",
       "L 122.029337 131.841275 \n",
       "L 125.226441 132.537984 \n",
       "L 128.423546 132.99568 \n",
       "L 131.354225 133.199585 \n",
       "L 134.284904 133.194618 \n",
       "L 137.215583 132.980839 \n",
       "L 140.412687 132.512579 \n",
       "L 143.609792 131.805672 \n",
       "L 146.806896 130.870286 \n",
       "L 150.270426 129.614798 \n",
       "L 154.000381 128.004962 \n",
       "L 157.996762 126.018647 \n",
       "L 162.525993 123.495941 \n",
       "L 167.854501 120.254292 \n",
       "L 176.646538 114.592157 \n",
       "L 184.372874 109.714269 \n",
       "L 189.434956 106.775457 \n",
       "L 193.697762 104.551947 \n",
       "L 197.427718 102.84283 \n",
       "L 200.891247 101.484286 \n",
       "L 204.354777 100.369361 \n",
       "L 207.551882 99.572776 \n",
       "L 210.748986 99.011309 \n",
       "L 213.679665 98.710134 \n",
       "L 216.610345 98.616988 \n",
       "L 219.541024 98.732998 \n",
       "L 222.471703 99.05676 \n",
       "L 225.668807 99.642233 \n",
       "L 228.865912 100.461824 \n",
       "L 232.063016 101.503746 \n",
       "L 235.526546 102.865935 \n",
       "L 239.256501 104.57854 \n",
       "L 243.252882 106.658771 \n",
       "L 247.782113 109.265085 \n",
       "L 253.643472 112.911134 \n",
       "L 270.42827 123.52761 \n",
       "L 274.957502 126.047244 \n",
       "L 278.953882 128.030159 \n",
       "L 282.683837 129.636311 \n",
       "L 286.147367 130.887999 \n",
       "L 289.344472 131.81961 \n",
       "L 292.541576 132.522541 \n",
       "L 295.738681 132.986683 \n",
       "L 298.66936 133.196612 \n",
       "L 301.600039 133.197704 \n",
       "L 304.530718 132.989947 \n",
       "L 307.727823 132.528129 \n",
       "L 310.924927 131.827441 \n",
       "L 314.122032 130.89796 \n",
       "L 317.585562 129.648417 \n",
       "L 321.049091 128.167191 \n",
       "L 325.045472 126.203027 \n",
       "L 329.308278 123.854559 \n",
       "L 334.37036 120.807714 \n",
       "L 341.830271 116.030232 \n",
       "L 341.830271 116.030232 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 174.6 224.64 \n",
       "L 174.6 7.2 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 342 224.64 \n",
       "L 342 7.2 \n",
       "\" style=\"fill:none;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 7.2 115.92 \n",
       "L 342 115.92 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 7.2 7.2 \n",
       "L 342 7.2 \n",
       "\" style=\"fill:none;\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p861e7116c6\">\n",
       "   <rect height=\"217.44\" width=\"334.8\" x=\"7.2\" y=\"7.2\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 720x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(dpi=120)\n",
    "\n",
    "set_ticks_pass_zero(ax)\n",
    "\n",
    "ax.set_xlim(-PI2, PI2)\n",
    "ax.set_ylim(-PI2, PI2)\n",
    "\n",
    "# set_axis_pi(ax, Decimal('2'), Decimal('0.5'))\n",
    "\n",
    "x = np.arange(-PI2, PI2, 0.01)\n",
    "y = np.sin(x)\n",
    "ax.plot(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 坐标用 pi 标注"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _range_decimal(start: Decimal, stop: Decimal, step: Decimal):\n",
    "    if start >= stop:\n",
    "        raise RuntimeError(\"start >= stop can't be true!\")\n",
    "\n",
    "    res = []\n",
    "    num = start\n",
    "    while num < stop:\n",
    "        res.append(num)\n",
    "        num += step\n",
    "    \n",
    "    return res[1:]  # 返回一个全开区间，即不包括 start 和 stop 的区间\n",
    "\n",
    "\n",
    "def _get_label(n: Decimal):\n",
    "    a, b = n.as_integer_ratio()\n",
    "    \n",
    "    if b == 1:  # 分母为 1，即为整数\n",
    "        if n == Decimal('0'):\n",
    "            return '0'\n",
    "        elif abs(n) == Decimal('1'):  # n 为 1\n",
    "            return r'$\\pi$' if n >= 0 else r'$-\\pi$'\n",
    "        else:\n",
    "            return rf'${a} \\pi$'\n",
    "    else: # 为分数\n",
    "        return r'$\\frac{'+ str(a) + '}{' + str(b) + '} \\pi$'\n",
    "\n",
    "    \n",
    "def set_axis_pi(axes, n: Decimal, step: Decimal):\n",
    "    \"\"\"\n",
    "    x/y 坐标轴的 limit 为 -n*pi 到 n*pi\n",
    "    tick 的间隔为 step*pi\n",
    "    \"\"\"\n",
    "    n_pi = float(n) * np.pi\n",
    "    \n",
    "    # 设置坐标轴的 limit\n",
    "    axes.set_xlim(-n_pi, n_pi)\n",
    "    axes.set_ylim(-n_pi, n_pi)\n",
    "    \n",
    "    # 设置 ticks\n",
    "    ticks_without_pi = _range_decimal(-n, Decimal('0'), step) + _range_decimal(Decimal('0'), n , step)  # 这个是 pi 的系数 range\n",
    "    ticks = [float(it)*np.pi for it in ticks_without_pi]  # 乘上pi\n",
    "    \n",
    "    labels = list(map(_get_label, ticks_without_pi))  # 标签\n",
    "    \n",
    "    # 设置 x/y 轴的坐标\n",
    "    axes.set_xticks(ticks)\n",
    "    axes.set_xticklabels(labels)\n",
    "    axes.set_yticks(ticks)\n",
    "    axes.set_yticklabels(labels)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
